package com.dawn.module.hospital.controller.app.chs;

import com.dawn.framework.apisign.core.annotation.Sign;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.module.hospital.controller.app.chs.vo.ChsOrderHisFeeReqVO;
import com.dawn.module.hospital.controller.app.chs.vo.ChsOrderUnifiedReqVO;
import com.dawn.module.hospital.enums.BHConstants;
import com.dawn.module.hospital.enums.ErrorCodeConstants;
import com.dawn.module.hospital.service.BHService;
import com.dawn.module.hospital.service.ChsOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


/**
 * @description:
 * @author: cnsu-cmh
 * @time: 2024/8/8 12:47
 */
@Tag(name = "医保移动支付 APP - bh")
@RestController
@RequestMapping("/bh/chs")
@Validated
public class AppChsOrderController {


    @Autowired
    private ChsOrderService chsOrderService;

    @Sign
    @PostMapping(value = "/fee")
    @Operation(summary = "获取医保分账数据")
    public CommonResult hisChsFee(@Valid @RequestBody ChsOrderHisFeeReqVO reqVO){
        return chsOrderService.hisChsFee(reqVO);
    }

    @Sign
    @PostMapping(value = "/pay")
    @Operation(summary = "支付下单")
    public CommonResult pay(@Valid @RequestBody ChsOrderUnifiedReqVO reqVO){
        // 微信公众号
        if (BHConstants.WEIXIN_SERVICE_ACCOUNTS.getCode().equals(reqVO.getAppType())) {
            return chsOrderService.wxPay(reqVO);
        }

        // 支付宝小程序
        if (BHConstants.ALIPAY_MINI_APPLET.getCode().equals(reqVO.getAppType())) {
            if (StringUtils.isBlank(reqVO.getMedicalCardInstId())) {
                return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(), "请传入医保卡机构ID");
            }

            if (StringUtils.isBlank(reqVO.getMedicalCardId())) {
                return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(), "请传入医保卡ID");
            }
            return chsOrderService.aliMiniPay(reqVO);
        }

        return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"应用类型不正确");
    }

}
